﻿using System;
using System.Linq;
using System.IO;
using System.Data;
using System.Data.SqlClient;

namespace liwuyou.sprider
{
    class common
    {
        private static SqlConnection con;

        //系统配置数据库文件名
        public const  String CONFIG_DB_NAME = "config.db";

        #region 字段读取
        //数据库主机名
        public static string DBHost { get; set; }

        //数据库名称
        public static string DBName { get; set; }

        //数据库用户名
        public static string DBUserName { get; set; }

        //数据库密码
        public static string DBPwd { get; set; }

        #endregion

        /// <summary>
        /// 检查是否存在系统设置数据库文件
        /// </summary>
        /// <returns></returns>
        public static Boolean CheckCfgDBExit()
        {
            String dbPath = System.Windows.Forms.Application.StartupPath + "\\" + CONFIG_DB_NAME;
            return File.Exists(dbPath) ? true : false;
        }

        /// <summary>
        /// 读取管家数据库设置
        /// </summary>
        /// <returns></returns>
        public static IQueryable ReadConfigDBToQuery()
        {
            SysCfgDataContext syscontext = new SysCfgDataContext();
            var query = from cfg in syscontext.SysConfigs
                        where cfg.Group == "db"
                        select cfg;
            //赋值到字段
            foreach (SysConfig item in query)
            {
                switch (item.Code)
                {
                    case "db_host":
                        DBHost = item.Value.Trim();
                        break;

                    case "db_name":
                        DBName = item.Value.Trim();
                        break;

                    case "db_username":
                        DBUserName = item.Value.Trim();
                        break;

                    case "db_pwd":
                        DBPwd = item.Value.Trim();
                        break;
                }
            }

            return query;
        }

        /// <summary>
        /// 管家数据库连接测试
        /// </summary>
        /// <returns></returns>
        public static Boolean ConnectTest()
        {
            var query = common.ReadConfigDBToQuery();

            if(!Connect.NetPortOp.ConnValidate(DBHost, 1433, 1000))
            {
               return false;
            }

            string gs_ConnectionString = "Server=" + DBHost + ";Persist Security Info=True;uid=" + DBUserName +
                                        ";password=" + DBPwd + ";Initial Catalog=" + DBName + ";Connect Timeout=20";
            try
            {
                con = new SqlConnection(gs_ConnectionString);
                con.Open();
            }
            catch (Exception) { }

            if (con.State == ConnectionState.Open)
            {
                con.Close();
                return true;
            }
            else
                return false;
        }
    }
}
